// 版权归陈超所有


#include "UI/HUD/CC_HUD.h"
#include "UI/Widget/CC_UserWidget.h"
#include "UI/WidgetController/CC_AttributeMenuWidgetController.h"
#include "UI/WidgetController/CC_OverlayWidgetController.h"

UCC_OverlayWidgetController* ACC_HUD::GetOverlayWidgetController(const FWidgetControllerParams& FWCParams)
{
	if (OverlayWidgetController == nullptr)	
	{
		OverlayWidgetController = NewObject<UCC_OverlayWidgetController>(this, OverlayWidgetControllerClass);		//创建覆盖层小部件控制器
		OverlayWidgetController->SetWidgetControllerParams(FWCParams);		//将参数赋值给小部件控制器
	}
	return OverlayWidgetController;
}

UCC_AttributeMenuWidgetController* ACC_HUD::GetAttributeMenuWidgetController(const FWidgetControllerParams& FWCParams)
{
	if (AttributeMenuWidgetController == nullptr)	
	{
		AttributeMenuWidgetController = NewObject<UCC_AttributeMenuWidgetController>(this, AttributeMenuWidgetControllerClass);		
		AttributeMenuWidgetController->SetWidgetControllerParams(FWCParams);		
	}
	return AttributeMenuWidgetController;
}

void ACC_HUD::InitOverlay(APlayerController* PC, APlayerState* PS, UAbilitySystemComponent* ASC, UAttributeSet* AS)
{
	checkf(OverlayWidgetClass,TEXT("OverlayWidgetClass没有设定！"));
	checkf(OverlayWidgetControllerClass,TEXT("OverlayWidgetControllerClass没有设定！"));

	//1.创建小部件
	UUserWidget* Widget = CreateWidget<UUserWidget>(GetWorld(), OverlayWidgetClass);
	OverlayWidget = Cast<UCC_UserWidget>(Widget);
	
	//2.创建小部件控制器
	const FWidgetControllerParams WidgetControllerParams = FWidgetControllerParams(PC, PS, ASC, AS);
		//获取覆盖层小部件控制器指针
	OverlayWidgetController = GetOverlayWidgetController(WidgetControllerParams);
		//小部件控制器监听属性变化,触发回调
	OverlayWidgetController->BindCallbacksToDependencies();

	//3.将小部件控制器添加到小部件
	OverlayWidget->AddWidgetController(OverlayWidgetController);

	//4.小部件控制器广播初始值
	OverlayWidgetController->BroadcastInitialValues();
	
	//将覆盖层小部件打印到屏幕
	OverlayWidget->AddToViewport();
}
